package cn.com.nes.site.action.design;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.design.CalculateCostVo;
import cn.com.nes.site.module.service.design.CalculateService;
import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("calculate")
@RequestMapping("/calculate")
public class CalculateAction {

    @Resource
    private CalculateService service;

    // 获取省份地址分类
    @RequestMapping(value = "/getCityTreeOfWeatherParamManage", method = RequestMethod.GET)
    @ResponseBody
    public PageData getCityTreeOfWeatherParamManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getCityTreeOfWeatherParamManage(req);
        return res;
    }

    // 获取气象参数详情
    @RequestMapping(value = "/getDetailsOfWeatherParamManage", method = RequestMethod.GET)
    @ResponseBody
    public PageData getDetailsOfWeatherParamManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getDetailsOfWeatherParamManage(req);
        return res;
    }

    // 获取围护结构分类
    @RequestMapping(value = "/getTypeTreeOfBuildingEnvelopeManage",method = RequestMethod.GET)
    @ResponseBody
    public PageData getTypeTreeOfBuildingEnvelopeManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getTypeTreeOfBuildingEnvelopeManage(req);
        return res;
    }

    // 获取围护结构详情
    @RequestMapping(value = "/getDetailsOfOfBuildingEnvelopeManage", method = RequestMethod.GET)
    @ResponseBody
    public PageData getDetailsOfOfBuildingEnvelopeManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getDetailsOfOfBuildingEnvelopeManage(req);
        return res;
    }

    // 获取材质管理分类
    @RequestMapping(value = "/getTypeTreeOfMaterialQualityManage",method = RequestMethod.GET)
    @ResponseBody
    public PageData getTypeTreeOfMaterialQualityManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getTypeTreeOfMaterialQualityManage(req);
        return res;
    }

    // 获取材质管理明细
    @RequestMapping(value = "/getDetailsOfMaterialQualityManage",method = RequestMethod.GET)
    @ResponseBody
    public PageData getDetailsOfMaterialQualityManage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.getDetailsOfMaterialQualityManage(req);
        return res;
    }

    // 保存材质
    @RequestMapping(value = "/saveMaterialQuality",method = RequestMethod.POST)
    @ResponseBody
    public PageData saveMaterialQuality(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = service.saveMaterialQuality(req);
        return res;
    }

    // 计算热负荷
    @RequestMapping(value = "/calculateHeatLoad",method = RequestMethod.POST)
    @ResponseBody
    public PageData calculateHeatLoad(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.calculateHeatLoad(req);
        return res;
    }

    // 计算
    @RequestMapping(value = "/calculateAndSave",method = RequestMethod.POST)
    @ResponseBody
    public PageData calculateAndSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res = new PageData();
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        CalculateCostVo vo = new CalculateCostVo(req);

        int i = (CollUtil.isNotEmpty(vo.getFdsj()) ? vo.getFdsj().size() : 0) +
                (CollUtil.isNotEmpty(vo.getGdsj()) ? vo.getGdsj().size() : 0) +
                (CollUtil.isNotEmpty(vo.getPdsj()) ? vo.getPdsj().size() : 0) +
                (CollUtil.isNotEmpty(vo.getJdsj()) ? vo.getJdsj().size() : 0);

        if (i != 24) {
            res.setRetCode(9);
            res.setRetMessage("参数错误");
            return res;
        }
        if ((vo.getFdRatio() == null ? 0 : vo.getFdRatio())
                + (vo.getGdRatio() == null ? 0 : vo.getGdRatio())
                + (vo.getPdRatio() == null ? 0 : vo.getPdRatio())
                + (vo.getJdRatio() == null ? 0 : vo.getJdRatio()) != 100) {
            res.setRetCode(9);
            res.setRetMessage("电占比参数错误");
            return res;
        }

        res = service.calculateAndSave(req, vo);
        return res;
    }

    @RequestMapping(value = "/getCalculate",method = RequestMethod.POST)
    @ResponseBody
    public PageData getCalculate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.getCalculate(req);
        return res;
    }

    @RequestMapping(value = "/deleteCalculate",method = RequestMethod.POST)
    @ResponseBody
    public PageData deleteCalculate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        res = service.deleteCalculate(req);
        return res;
    }

}
